//
//  CQApplyViewController.m
//  HiChat
//
//  Created by wqiong on 16/1/21.
//  Copyright © 2016年 wqiong. All rights reserved.
//

#import "CQApplyViewController.h"

@interface CQApplyViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *titleArray;

@end

@implementation CQApplyViewController

- (void)dealloc
{
    [_tableV release];
    [_titleArray release];
    self.tableV.delegate = nil;
    self.tableV.dataSource = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatTabView];
    
    self.titleArray = [NSMutableArray arrayWithObjects:@"wang",@"dsd", nil];
}

- (void)creatTabView
{
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.tableV.frame];
    imageV.image = [UIImage imageNamed:@"apply"];
    self.tableV.backgroundView = imageV;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return [self.titleArray objectAtIndex:section];
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableV.bounds.size.width, 30)];
    view.backgroundColor = [UIColor clearColor];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, self.tableV.bounds.size.width, 30)];
    NSString *name = [self.titleArray objectAtIndex:section];
    NSString *nameText = [NSString stringWithFormat:@"%@请求加您为好友",name];
    titleLabel.text = nameText;
    [view addSubview:titleLabel];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
    imageView.tag = 20000+section;
    
    imageView.image = [UIImage imageNamed:@"touxiang1"];
    [view addSubview:imageView];
    
    //添加一个button 用来监听点击分组，实现分组的展开关闭。
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(0, 0, self.tableV.bounds.size.width, 50);
    btn.tag=10000+section;
    [btn addTarget:self action:@selector(btnOpenList:) forControlEvents:UIControlEventTouchDown];
    [view addSubview:btn];
    
    return view;
}

-(void)btnOpenList:(UIButton *)sender
{
//    NSString *string = [NSString stringWithFormat:@"%d",sender.tag-10000];
    
    //数组selectedArr里面存的数据和表头想对应，方便以后做比较
//    if ([selectedArr containsObject:string])
//    {
//        [selectedArr removeObject:string];
//    }
//    else
//    {
//        [selectedArr addObject:string];
//    }
    NSLog(@"11111");
    [self.tableV reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellApply = @"cellApply";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellApply];
    
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellApply] autorelease];
        cell.backgroundColor = [UIColor clearColor];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"同意";
    } else {
        cell.textLabel.text = @"拒绝";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = [self.titleArray objectAtIndex:indexPath.section];
    if (indexPath.row == 0) {
                //同意加好友申请
                EMError *error = nil;
                BOOL isSuccess = [[EaseMob sharedInstance].chatManager acceptBuddyRequest:name error:&error];
                if (isSuccess && !error) {
                    NSLog(@"发送同意成功");
                }
    }
    if (indexPath.row == 1) {
                //拒绝加好友申请
                EMError *error = nil;
                BOOL isSuccess = [[EaseMob sharedInstance].chatManager rejectBuddyRequest:name reason:@"你长得太丑" error:&error];
                if (isSuccess && !error) {
                    NSLog(@"发送拒绝成功");
                }
    }
}

/*!
 @method
 @brief 接收到好友请求时的通知
 @discussion
 @param username 发起好友请求的用户username
 @param message  收到好友请求时的say hello消息
 */
- (void)didReceiveBuddyRequest:(NSString *)username
                       message:(NSString *)message
{
    [self.titleArray addObject:username];
    NSUserDefaults *useD = [NSUserDefaults standardUserDefaults];
    [useD setObject:self.titleArray forKey:@"apply"];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
